﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class Invoice : BasePage
    {
        protected Button btnCreateInvoice;
        protected Button btnGoBack;
        protected Button btnNew;
        protected HtmlForm F9211;
        protected GridView gvInvoiceDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlHead Head1;
        protected HtmlImage imgStatementID;
        protected Label lblBillingDate;
        protected Label lblInvoiceID;
        protected Label lblMsg;
        protected Label lblOnwerID;
        protected Label lblOwner;
        protected Label lblStatementID;
        protected Label lblTotalAmount;
        protected Label lblWhName;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBillingDate;
        protected TextBox txtInvoiceID;
        protected TextBox txtOwnerID;
        protected TextBox txtStatementID;
        protected TextBox txtTotalAmount;
        protected TextBox txtWhName;
        protected UpdatePanel up1;

        protected void btnCreateInvoice_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtStatementID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_001");
            }
            else
            {
                this.InsertInvoiceInfo();
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("InvoiceQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("InvoiceList.aspx?from=edit", false);
                    break;
            }
        }

        protected void gvInvoiceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as InvoiceInfo).InvoiceDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvInvoiceDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvInvoiceDetail.PageIndex * this.gvInvoiceDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvInvoiceDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                InvoiceInfo invoiceInfo = base.DataEntityViewState as InvoiceInfo;
                DataView dv = CADataConverter.ToDataTable(invoiceInfo.InvoiceDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvInvoiceDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertInvoiceInfo()
        {
            try
            {
                InvoiceInfo InvoiceInfo = new InvoiceInfo {
                    StatementID = this.txtStatementID.Text,
                    CreatedBy = base.UserInfo.UserID
                };
                this.txtInvoiceID.Text = BillingServiceClientProxy.GetInstance().InsertInvoice(InvoiceInfo);
                base.DataEntityViewState = InvoiceInfo;
                this.SetInvoiceInfo2Form();
                this.lblMsg.Text = this.txtInvoiceID.Text + base.GetGlobalMsgResource("F9210_001");
                this.txtInvoiceID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_003") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.txtInvoiceID.Text = base.Request.QueryString["ID"].ToString();
                this.SetInvoiceInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetInvoiceInfo2Form()
        {
            try
            {
                InvoiceInfo InvoiceInfo = BillingServiceClientProxy.GetInstance().GetInvoiceByID(this.txtInvoiceID.Text);
                if (InvoiceInfo != null)
                {
                    this.txtInvoiceID.Text = InvoiceInfo.InvoiceID;
                    this.txtInvoiceID.Enabled = false;
                    this.txtOwnerID.Text = InvoiceInfo.OwnerID;
                    this.txtWhName.Text = InvoiceInfo.WhName;
                    this.txtStatementID.Text = InvoiceInfo.StatementID;
                    this.txtStatementID.Enabled = false;
                    this.imgStatementID.Visible = false;
                    this.txtBillingDate.Text = base.GetPreferenceShortDate(InvoiceInfo.BillingDate);
                    this.txtTotalAmount.Text = "0";
                    this.btnCreateInvoice.Enabled = false;
                    if ((InvoiceInfo.InvoiceDetailInfos != null) && (InvoiceInfo.InvoiceDetailInfos.Count > 0))
                    {
                        decimal dAmount = 0M;
                        foreach (InvoiceDetailInfo info in InvoiceInfo.InvoiceDetailInfos)
                        {
                            dAmount += info.Amount;
                        }
                        this.txtTotalAmount.Text = base.GetPreferenceDigit(dAmount);
                    }
                    base.BindData2GridView(this.gvInvoiceDetail, InvoiceInfo.InvoiceDetailInfos);
                    base.DataEntityViewState = InvoiceInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }
    }
}

